﻿using System;
using System.Drawing;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Controls;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.SystemUI;
using ESRI.ArcGIS.esriSystem;

namespace MidTermWorkWPF.MyTools
{
    /// <summary>
    /// Summary description for MyAddData.
    /// </summary>
    [Guid("0c3aead0-d635-40ff-9726-43465c2c57b6")]
    [ClassInterface(ClassInterfaceType.None)]
    [ProgId("MidTermWorkWPF.MyMenuItems.MyAddDateTool.MyAddDateToolClass")]
    public sealed class MyAddDateToolClass : BaseTool
    {
        #region COM Registration Function(s)
        [ComRegisterFunction()]
        [ComVisible(false)]
        static void RegisterFunction(Type registerType)
        {
            // Required for ArcGIS Component Category Registrar support
            ArcGISCategoryRegistration(registerType);

            //
            // TODO: Add any COM registration code here
            //
        }

        [ComUnregisterFunction()]
        [ComVisible(false)]
        static void UnregisterFunction(Type registerType)
        {
            // Required for ArcGIS Component Category Registrar support
            ArcGISCategoryUnregistration(registerType);

            //
            // TODO: Add any COM unregistration code here
            //
        }

        #region ArcGIS Component Category Registrar generated code
        /// <summary>
        /// Required method for ArcGIS Component Category registration -
        /// Do not modify the contents of this method with the code editor.
        /// </summary>
        private static void ArcGISCategoryRegistration(Type registerType)
        {
            string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
            ControlsCommands.Register(regKey);

        }
        /// <summary>
        /// Required method for ArcGIS Component Category unregistration -
        /// Do not modify the contents of this method with the code editor.
        /// </summary>
        private static void ArcGISCategoryUnregistration(Type registerType)
        {
            string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
            ControlsCommands.Unregister(regKey);

        }

        #endregion
        #endregion

        private IHookHelper m_hookHelper;

        public MyAddDateToolClass()
        {
            //
            // TODO: Define values for the public properties
            //
            base.m_category = "MyAddDate"; //localizable text 
            base.m_caption = "MyAddDate";  //localizable text 
            base.m_message = "click and add date";  //localizable text
            base.m_toolTip = "MyAddDate";  //localizable text
            base.m_name = "MyAddDate";   //unique id, non-localizable (e.g. "MyCategory_MyTool")
            try
            {
                //
                // TODO: change resource name if necessary
                //
                //string bitmapResourceName = GetType().Name + ".bmp";
                //base.m_bitmap = new Bitmap(GetType(), bitmapResourceName);
                //base.m_cursor = new System.Windows.Forms.Cursor(GetType(), GetType().Name + ".cur");

                // 获取程序集中的资源数组
                string[] res = GetType().Assembly.GetManifestResourceNames();
                // 遍历资源数组，找到你想要的图标文件
                foreach (string r in res)
                {
                    if (r.Contains(GetType().Name + ".bmp"))
                    {
                        // 设置工具属性
                        base.m_bitmap = new Bitmap(GetType().Assembly.GetManifestResourceStream(r));
                        break;
                    }
                }
                foreach (string r in res)
                {
                    if (r.Contains(GetType().Name + ".cur"))
                    {
                        // 设置工具属性
                        base.m_cursor = new System.Windows.Forms.Cursor(GetType().Assembly.GetManifestResourceStream(r));
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap");
            }

              
        }

        #region Overridden Class Methods

        /// <summary>
        /// Occurs when this tool is created
        /// </summary>
        /// <param name="hook">Instance of the application</param>
        public override void OnCreate(object hook)
        {
            if (m_hookHelper == null)
                m_hookHelper = new HookHelperClass();

            m_hookHelper.Hook = hook;

            // TODO:  Add MyAddData.OnCreate implementation
        }

        /// <summary>
        /// Occurs when this tool is clicked
        /// </summary>
        public override void OnClick()
        {
            // TODO: Add MyAddData.OnClick implementation
        }

        public override void OnMouseDown(int Button, int Shift, int X, int Y)
        {
         // TODO: 添加 AddDateTool.OnMouseDown 实现
         base.OnMouseDown (Button, Shift, X, Y);

         // 获取活动视图
         IActiveView activeView = m_hookHelper.ActiveView;

         // 创建新的文本元素
         ITextElement textElement = new TextElementClass();

         // 创建文本符号
         ITextSymbol textSymbol = new TextSymbolClass();
         textSymbol.Size = 10;

         // 设置文本元素属性
         textElement.Symbol = textSymbol;
         textElement.Text = DateTime.Now.ToShortDateString();

         // 对IElementQI
         IElement element = (IElement) textElement;

         // 创建页点
         IPoint point = new PointClass();
         point = activeView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y);

         // 设置元素图形
         element.Geometry = point;

         // 增加元素到图形绘制容器
         activeView.GraphicsContainer.AddElement(element, 0);

         // 刷新图形
         activeView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
     }

        public override void OnMouseMove(int Button, int Shift, int X, int Y)
        {
            // TODO:  Add MyAddData.OnMouseMove implementation
        }

        public override void OnMouseUp(int Button, int Shift, int X, int Y)
        {
            // TODO:  Add MyAddData.OnMouseUp implementation
        }
        #endregion
    }
}
